<template>
    <el-divider/>
    <el-icon>
        <DocumentChecked/>
    </el-icon>
    <span>发药</span>
    <el-divider/>
    <div style="margin-bottom: 1vw">患者信息查询</div>
    <span>病历号：</span>
    <el-input v-model="patientInfo.caseNumber" placeholder="Please input" style="width: 20vw;margin-right: 2vw"/>
    <span>患者姓名：</span>
    <el-input v-model="patientInfo.realName" placeholder="Please input" style="width: 20vw;margin-right: 2vw"/>
    <el-button type="primary" @click="searchPatient()">搜索</el-button>
    <el-divider/>
  <!--
       查询病人
       -->
    <el-table :data="patientInfoList" stripe table-layout="auto">
        <el-table-column label="患者病历号">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.caseNumber }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="患者姓名">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.realName }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="性别">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.gender }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="年龄">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <span style="margin-left: 10px">{{ scope.row.age }}</span>
                    <span style="margin-left: 10px">{{ scope.row.ageType }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column>
            <template #default="scope">
                <el-button type="primary" @click="queryDrugList()">显示药品</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-divider/>
    <el-table
            ref="multipleTableRef"
            :data="drugInfoList"
            style="width: 100%"
    >
        <el-table-column property="drugCode" label="药品编码" width="240"/>
        <el-table-column property="drugName" label="药品名称"/>
        <el-table-column property="drugFormat" label="药品规格"/>
        <el-table-column property="drugUnit" label="包装单位"/>
        <el-table-column property="manufacturer" label="生产厂家" width="240"/>
        <el-table-column property="drugPrice" label="单价"/>
        <el-table-column property="drugNumber" label="数量"/>
        <el-table-column>
            <template #default="scope">
                <el-button @click="giveDrug(scope.row)">发药</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

<script setup>
import {DocumentChecked} from "@element-plus/icons-vue";
import {ref, reactive, provide} from "vue";
import axios from "axios";

//当前医生中正在看诊的患者列表
const patientInfoList = ref([])
const drugInfoList = ref([])

const patientInfo = ref({
    caseNumber: '',
    realName: '',
    age: '',
    cardNumber: '',
    gender: ''
})

function queryDrugList() {
    axios({
        method: 'get',
        url: 'http://localhost:8091/DrugInfoDto/queryDrugWithPrescriptionId/' + patientInfo.value.id + '/已缴费'
    }).then(resp => {
        console.log(resp.data.data)
        drugInfoList.value = resp.data.data
    })
}

//根据条件搜索患者信息
function searchPatient() {
    axios({
        method: 'post',
        url: 'http://localhost:8092/register/queryPatientByNameOrCaseNum',
        data: patientInfo.value
    }).then(resp => {
        if (!resp.data.success) {
            return
        }
        patientInfo.value = resp.data.data
        patientInfoList.value[0] = patientInfo.value
        console.log(patientInfo.value)
    })
}

function giveDrug(val) {
    console.log(val)
    let drugList = []
    drugList.push(val.id)
    console.log(patientInfo.value)
    axios({
        method: "post",
        url: 'http://localhost:8092/prescription/giveDrug/' + patientInfo.value.id,
        data: drugList
    }).then(resp => {
        if (resp.data.success) {
            window.alert("发药成功")
            queryDrugList()
        }
    })
}
</script>

<style scoped>

</style>